#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> 


int main(void)
{
    int fd;
    char *p = "test of unlink\n";
    char *p2 = "after write something.\n";

    fd = open("temp.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
        perror("open temp error");
        exit(1);
    }


    int ret = write(fd, p, strlen(p));
    if (ret == -1) {
        perror("-----write error");
    }

    // Segmentation fault (core dumped) 会导致段错误
    // p[0] = 'H';

    printf("hi! printf\n");
    ret = write(fd, p2, strlen(p2));
    if (ret == -1) {
        perror("-----write error");
    }

    printf("Enter anykey continue\n");
    getchar();

    close(fd);

    ret = unlink("temp.txt"); // 具备了释放的条件
    if (ret < 0) {
        perror("unlink error");
        exit(1);
    }

    return 0;

}